package com.cssw.fyzb.core.dao.user;

import com.cssw.fyzb.core.model.dodb.UserRel;
import com.ydn.dbframe.plugin.activerecord.Db;
import com.ydn.dbframe.plugin.activerecord.parse.SqlParse;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class UserRelDaoImpl implements UserRelDao {
    @Override
    public Integer getFansNum(Long userId) {
        String sql = "select count(1) total from t_user_rel where rel_user_id=? ";
        return Db.queryInt(sql, userId);
    }

    @Override
    public Integer getFollowNum(Long userId) {
        String sql = "select count(1) total from t_user_rel where user_id=? ";
        return Db.queryInt(sql, userId);
    }

    @Override
    public Integer getFriendNum(Long userId) {
        String sql = "select count(1) total from t_user_rel where user_id=? and rel_type=1";
        return Db.queryInt(sql, userId);
    }

    @Override
    public List<UserRel> listFollow(Long userId) {
        Map<String, Object> param = new HashMap<>();
        param.put("userId", userId);
        SqlParse sqlParse = new SqlParse(param);
        sqlParse.addSQL("select * from t_user_rel where user_id = :userId");
        return UserRel.dao.find(sqlParse);
    }

    @Override
    public UserRel getByUIdAndRId(Long userId, Long relUserId) {
        String sql = "select * from t_user_rel where user_id = ? and rel_user_id = ?";
        return UserRel.dao.findFirst(sql, userId, relUserId);
    }

    @Override
    public void delete(Long id) {
        UserRel.dao.deleteById(id);
    }

    @Override
    public Long save(Long userId, Long relUserId, Integer relType) {
        UserRel userRel = new UserRel();
        userRel.setUserId(userId);
        userRel.setRelUserId(relUserId);
        userRel.setRelType(relType);
        userRel.save();
        return userRel.getId();
    }

    @Override
    public void updateRelType(Long id, Integer relType) {
        UserRel userRel = new UserRel();
        userRel.setId(id);
        userRel.setRelType(relType);
        userRel.update();
    }

    @Override
    public void updateRelType(Long userId, Long relUserId, Integer relType) {
        String sql = "update t_user_rel set rel_type = ? where user_id = ? and rel_user_id = ?";
        Db.update(sql, relType, userId, relUserId);
    }

    @Override
    public Long getRelType(Long userId, Long relUserId) {
        String sql = "select rel_type from t_user_rel where user_id = ? and rel_user_id = ?";
        return Db.queryLong(sql, userId, relUserId);
    }

    @Override
    public Integer getYbFansNum(Long userId) {
        String sql = "select count(1) from cmf_user_attention where  touid=?";
        return Db.queryInt(sql, userId);
    }


}
